package com.happy.zebra.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.zebra.admin.api.entity.SysDict;
import com.happy.zebra.admin.mapper.SysDictMapper;
import com.happy.zebra.admin.service.SysDictService;
import com.shycloud.mido.basic.api.dto.DictionaryDTO;
import com.shycloud.mido.basic.api.entity.BasDictionary;
import com.shycloud.mido.common.core.util.R;

import lombok.AllArgsConstructor;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author nianhua.jiang
 * @since 2021-06-09
 */
@Service
@AllArgsConstructor
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

	/** 字典表业务 mapper */
	private SysDictMapper sysDictMapper;

	@Override
	public Map<String, Map<String, String>> getDicMap(List<String> types) {
		Map<String, Map<String, String>> resMap = new HashMap<>();
		for (String type : types) {
			Map<String, String> item = new HashMap<>();
			List<SysDict> list = this.getDicByType(type).getData();
			for (int i = 0; i < list.size(); i++) {
				item.put(list.get(i).getValue(), list.get(i).getLabel());
			}
			resMap.put(type, item);
		}
		return resMap;
	}

	/**
	 * 字典表查询REDIS
	 * @author bard.bai
	 * @param type 类别
	 * @return Map<String, String> map
	 */
	@Cacheable(value = "dict_details", key = "#type")
	@Override
	public R<List<SysDict>> getDicByType(String type) {
		System.out.println("缓存KEY："+type);
		return new R<List<SysDict>>(this.list(Wrappers
				.<SysDict>query().lambda()
				.eq(SysDict::getType, type)));
	}
	/**
	 * 字典表查询
	 *
	 * @param type 类别
	 * @return Map<String, String> map
	 */
	@Cacheable(value = "dict_details", key = "#type")
	public Map<String, String> dicMap(String type) {
		Map<String, String> map = new HashMap<>();
		List<SysDict> list = sysDictMapper.selectList(new QueryWrapper<SysDict>().eq("type", type));
		for (int i = 0; i < list.size(); i++) {
			map.put(list.get(i).getValue(), list.get(i).getLabel());
		}
		return map;
	}

}
